<?php
require_once '../../util/ConfigUtil.php';
require_once '../../util/ServiceUtil.php';
require_once '../../util/ThumbHandler.php';

$myUploader = new Uploader();

class Uploader
{
	public static $TYPE_IMAGE 	= "image";
 	public static $TYPE_PDF 	= "pdf";
 	public static $TYPE_SWF 	= "swf";
 	public static $TYPE_TESTCODE = "testcode";
 	
 	public static $BASE_PATH 	= "uploadFiles/";
 	public static $DEST_IMAGE_TN= "tn/";
 	public static $TN_WIDTH		= 40;
 	public static $TN_HEIGHT	= 40;
 	
 	protected $fileUploader = NULL;
 	protected $category 	= NULL;
 	protected $fileDest 	= NULL;
 	protected $fileFullPath = NULL;
 	
 	protected $myDb;//mysql_connect
	protected $sql;//string
	protected $query;//string
	protected $numRows;//int
	protected $result;//array
 	
	public function Uploader()
	{
		$this->fileUploader = $_REQUEST["email"];
		$this->category		= $_REQUEST["ano"];
		
		$this->myDb = null;
		$this->sql = null;
		$this->query = null;
		$this->numRows = 0;
		$this->result = array();
		$this->execute();
	}
	
	protected function execute()
	{
		try {
			$this->myDb = mysql_connect(ConfigUtil::SERVER_NAME(),ConfigUtil::MYSQL_USERNAME(),ConfigUtil::MYSQL_PASSWORD());
			mysql_select_db(ConfigUtil::MYSQL_TABLENAME(),$this->myDb);
			mysql_query("SET NAMES 'utf8'");
//			echo "InitializeDB success!";
			$this->checkUploader($this->fileUploader);//step01
		}catch (Exception $exc)
		{
			trigger_error("InitializeDB error!".$exc);
		}
	}
	//step01
	public function checkUploader($uploader)
	{
		if(!is_null($uploader))
		{
			$email = strtolower(trim($uploader));
			$this->sql = "SELECT students.email FROM students WHERE email='$email'";
			$this->query = mysql_query($this->sql,$this->myDb);
			$this->numRows = mysql_num_rows($this->query);
			if($this->numRows>=1)
			{
				$this->checkCategory($this->category);//step02
			}else
			{
				return "<error>Your wanna upload but your email not exist in our database!</error>";
			}
		}else
		{
			return "<error>Your wanna upload but your email not exist in our database!</error>";
		}
		return NULL;
	}
	//step02
	public function checkCategory($fileCategory)
	{
		if(!is_null($fileCategory))
		{
			$this->fileDest = $this->category."/";
			$this->executeUpload();//step03;
		}else
		{
			return "<error>Your wanna upload category invalid!</error>";
		}
		return NULL;
	}
	
	//step03
	public function executeUpload()
	{
		$fileName = $_FILES["Filedata"]["name"]; 
		$fileNameNew = strpbrk($fileName, '.');
		$filenameFinal=date("Ymd")."_".date("ahis").$fileNameNew;
		$fileSource = $_FILES["Filedata"]["tmp_name"]; 
		$fileNewLocation = self::$BASE_PATH.$this->fileDest.$filenameFinal;
		
		//execute file upload
		if(move_uploaded_file($fileSource,$fileNewLocation))
		{
			echo $filenameFinal;
			if($this->category==self::$TYPE_IMAGE)
			{
				//execute create thumbnail
				if(file_exists($fileNewLocation))
				{
					try {
						$thumbHandler = new ThumbHandler();
						$thumbHandler->setSrcImg($fileNewLocation);
				    	$thumbHandler->setCutType(1);//
				    	$newThumbnail = (self::$BASE_PATH).$this->fileDest.(self::$DEST_IMAGE_TN).$filenameFinal;
				   		$thumbHandler->setDstImg($newThumbnail);
				    	$thumbHandler->createImg(self::$TN_WIDTH,self::$TN_HEIGHT);
				    	
				    	try
				    	{
				    		$this->sql = "UPDATE albums SET albums.thumbnail='$newThumbnail' WHERE albums.ano='$ano'";
 							$this->query = mysql_query($sql,$_db);
				    	}catch(Exception $exc)
				    	{
				    		return $exc;
				    	}
					}catch (Exception $exc)
					{
						return "<fail>create thumbnail failure!".$exc."</fail>";
					}
				}else 
				{
					return "<fail>thumbnail source file not existed!</fail>";
				}
				
			}
		}else
		{
			return "<fail>upload failure</fail>";
		}
		return NULL;
	}
}

?>